#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2014-08-22 01:14:23
# @Author  : shitao.tommy (hero007asd@gmail.com)
# @Link    : http://example.org
# @Version : $Id$

from framework.db.basedb import BaseDb
from tornado import gen
from lib import log,libtime


class Out(BaseDb):

    @gen.coroutine
    def new(self, token='', brand='', part_no='', num=0, weight='', 
        data_code='', ship_time='', deliver_address='', client='', 
        client_pn='', order_no='', user='', pwd=''):
        '''添加出库信息'''
        resp = ''
        ins_sql = '''INSERT INTO qr_out_storage(Ftoken,Fbrand,Fpart_no,Fnum,Fweight,Fdata_code,Fship_time
                                ,Fdeliver_address, Fclient,Fclient_pn,Forder_no,Fuser,Fpwd,Fcreate_time)
                    VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,UNIX_TIMESTAMP())'''
        try:
            if ship_time != '':
                ship_time = libtime.get_start_timestamp(ship_time)
            else:
                ship_time = 0
            if num == '': num = 0
            param_list = [token,brand,part_no,int(num),weight,data_code,ship_time,deliver_address,client,client_pn,order_no,user,pwd]
            ret = yield self.upd_or_ins(ins_sql,tuple(param_list))
            if ret > 0:
                resp = '1'
            else: resp = '0'
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)



    @gen.coroutine
    def list(self, token='', brand='', data_code='', ship_time='', 
        deliver_address='', client='', client_pn='', order_no='',part_no='',  page=0, offset=20):
        '''查询出库信息列表'''
        resp = ''
        sel_sql = '''SELECT Fid, Ftoken,Fbrand,Fpart_no,Fnum,Fweight,Fdata_code,Fship_time
                                ,Fdeliver_address, Fclient,Fclient_pn,Forder_no,Fuser,Fpwd
                    FROM qr_out_storage
                    WHERE 1=1'''
        param_list = []
        sel_arr = [sel_sql]
        if token != '':
            sel_arr.append(' AND Ftoken = %s')
            param_list.append(token)
        if brand != '':
            sel_arr.append(' AND Fbrand = %s')
            param_list.append(brand)
        if data_code != '':
            sel_arr.append(' AND Fdata_code = %s')
            param_list.append(data_code)
        if ship_time != '':
            sel_arr.append(' AND Fship_time = %s')
            param_list.append(libtime.get_start_timestamp(ship_time))
        if deliver_address != '':
            sel_arr.append(' AND Fdeliver_address = %s')
            param_list.append(deliver_address)
        if client != '':
            sel_arr.append(' AND Fclient = %s')
            param_list.append(client)
        if client_pn != '':
            sel_arr.append(' AND Fclient_pn = %s')
            param_list.append(client_pn)
        if order_no != '':
            sel_arr.append(' AND Forder_no = %s')
            param_list.append(order_no)
        if part_no != '':
            sel_arr.append(' AND Fpart_no = %s')
            param_list.append(part_no)

        sel_arr.append(' ORDER BY Fid DESC LIMIT %s,%s')
        param_list.append(int(page))
        param_list.append(int(offset))
        sel_sql = ''.join(sel_arr)

        try:
            ret = yield self.execute(sel_sql,tuple(param_list))
            resp = []
            for i in ret:
                resp.append({'id':i[0],'token':i[1],'brand':i[2],'partNo':i[3],'num':i[4],'weight':i[5],'dataCode':i[6],'shipTime':i[7]
                    ,'deliverAddress':i[8], 'client':i[9], 'clientPn':i[10], 'orderNo':i[11], 'user':i[12], 'pwd':i[13]})
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)


    @gen.coroutine
    def update(self, fid='', token='', brand='', part_no='', num=0, weight='', 
        data_code='', ship_time=0, deliver_address='', client='', 
        client_pn='', order_no='', user='', pwd=''):
        '''修改出库信息'''
        resp = ''
        upd_sql = '''UPDATE qr_out_storage
                    SET Ftoken = %s
                    ,Fbrand = %s
                    ,Fpart_no = %s
                    ,Fnum = %s
                    ,Fweight = %s
                    ,Fdata_code = %s
                    ,Fship_time = %s
                    ,Fdeliver_address = %s
                    ,Fclient = %s
                    ,Fclient_pn = %s
                    ,Forder_no = %s
                    ,Fuser = %s
                    ,Fpwd = %s
                    WHERE Fid = %s'''
        try:
            if ship_time != '':
                ship_time = libtime.get_start_timestamp(ship_time)
            else:
                ship_time = 0
            param_list = [token,brand,part_no,int(num),weight,data_code,ship_time,deliver_address,client,client_pn,order_no,user,pwd, fid]

            ret = yield self.upd_or_ins(upd_sql,tuple(param_list))
            if ret > 0:
                resp = '1'
            else: resp = '0'
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)


    @gen.coroutine
    def delete(self, fid):
        '''删除出库信息'''
        resp = ''
        del_sql = '''DELETE FROM qr_out_storage WHERE Fid = %s'''
        try:
            ret = yield self.upd_or_ins(del_sql,(fid,))
            if ret > 0:
                resp = '1'
            else: resp = '0'
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)



    @gen.coroutine
    def dtl(self, fid=0):
        '''查询出库信息列表'''
        resp = ''
        sel_sql = '''SELECT Fid, Ftoken,Fbrand,Fpart_no,Fnum,Fweight,Fdata_code,Fship_time
                                ,Fdeliver_address, Fclient,Fclient_pn,Forder_no,Fuser,Fpwd
                    FROM qr_out_storage
                    WHERE Fid=%s'''

        try:
            ret = yield self.execute(sel_sql,(int(fid),))
            resp = {}
            for i in ret:
                resp = {'id':i[0],'token':i[1],'brand':i[2],'partNo':i[3],'num':i[4],'weight':i[5],'dataCode':i[6],'shipTime':i[7]
                    ,'deliverAddress':i[8], 'client':i[9], 'clientPn':i[10], 'orderNo':i[11], 'user':i[12], 'pwd':i[13]}
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)